Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports DotNetNuke.UI.Utilities

Namespace ModuleMasters.DNN.Modules.NCAAPoolBracket

    Public MustInherit Class NCAAPoolBracketEdit
        Inherits Entities.Modules.PortalModuleBase

#Region "Controls"
        Protected WithEvents txtR1Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtR2Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtR3Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtR4Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtR5Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtR6Points As System.Web.UI.WebControls.TextBox
        Protected WithEvents lnkSaveSettings As System.Web.UI.WebControls.LinkButton
        Protected WithEvents valR1Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents valR1PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR2Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents valR2PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR3Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents valR3PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR4PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR5PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR6PointsRegExp As System.Web.UI.WebControls.RegularExpressionValidator
        Protected WithEvents valR4Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents valR5Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents valR6Points As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents txtPickCutoff As System.Web.UI.WebControls.TextBox
        Protected WithEvents lblWrongDate As System.Web.UI.WebControls.Label
#End Region

#Region "Private Members"
        Private itemId As Integer
#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim ArrRoundPoints As ArrayList
                Dim GameController As New NCAAController

                If Not Page.IsPostBack Then

                    Me.lblWrongDate.Visible = False

                    ArrRoundPoints = GameController.GetRoundPoints(PortalId, ModuleId)

                    If ArrRoundPoints.Count = 0 Then
                        Me.txtR1Points.Text = 1
                        Me.txtR2Points.Text = 2
                        Me.txtR3Points.Text = 4
                        Me.txtR4Points.Text = 8
                        Me.txtR5Points.Text = 16
                        Me.txtR6Points.Text = 32
                    End If
                    'load points awarded for each round
                    Dim objRoundPoints As RoundInfo
                    Dim i As Integer
                    For i = 0 To ArrRoundPoints.Count - 1

                        objRoundPoints = CType(ArrRoundPoints(i), RoundInfo)
                        Dim c As Control
                        For Each c In Me.Controls
                            If Not c.ID Is Nothing Then
                                If c.ID.Substring(0, 4) = "txtR" Then
                                    Dim txt As System.Web.UI.WebControls.TextBox
                                    txt = CType(c, TextBox)
                                    If CInt(txt.ID.Substring(4, 1)) = objRoundPoints.RoundId Then
                                        txt.Text = objRoundPoints.RoundPoints
                                    End If
                                End If
                            End If
                        Next
                    Next

                    'load the setting
                    'Dim objSetting As New Entities.Modules.ModuleSettingsBase

                    If Not (Settings("PickCutoff") Is Nothing) Then
                        If Settings("PickCutoff") <> "" Then
                            Me.txtPickCutoff.Text = Settings("PickCutoff")
                        Else
                            Me.txtPickCutoff.Text = "3/19/2008 23:59"
                        End If
                    Else
                        Me.txtPickCutoff.Text = "3/19/2008 23:59"
                    End If
                End If

            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub lnkSaveSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkSaveSettings.Click
            'this will save a users pics to the DB, we will allow saving partial pics and will only score on what picks 
            'are there
            Dim GameController As New NCAAController
            Dim RoundId As Integer
            Dim RoundGames As Integer


            'determine if date is good
            If DateIsGood(Me.txtPickCutoff.Text) Then
                Me.lblWrongDate.Visible = False

                'delete existing records
                GameController.DeleteRoundPoints(PortalId, ModuleId)


                'for each control that is a game id, call the insertprediction
                Dim c As Control
                For Each c In Me.Controls
                    If Not c.ID Is Nothing Then
                        If c.ID.Substring(0, 4) = "txtR" Then
                            Dim txt As System.Web.UI.WebControls.TextBox
                            txt = CType(c, TextBox)
                            If Len(txt.Text) > 0 Then
                                RoundId = CInt(c.ID.Substring(4, 1))
                                RoundGames = Math.Pow(2, 6 - RoundId)
                                GameController.InsertRoundPoints(PortalId, ModuleId, RoundId, CInt(txt.Text), RoundGames)
                            End If
                        End If
                    End If
                Next

                'now save any additional settings
                Dim objModules As New Entities.Modules.ModuleController
                objModules.UpdateModuleSetting(ModuleId, "PickCutoff", Me.txtPickCutoff.Text)

                'redirect back to portal
                Response.Redirect(NavigateURL())
            Else
                Me.lblWrongDate.Visible = True
                Me.lblWrongDate.Text = "Incorrect Date Format"
            End If


        End Sub

#End Region

        Public Function DateIsGood(ByVal dtDate As String) As Boolean
            Try
                Dim dt As Date

                dt = CDate(dtDate)
                Return True

            Catch ex As Exception
                Return False
            End Try

        End Function


#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

    End Class

End Namespace
